package com.ld.admin.util.daliySentence.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.ld.admin.util.daliySentence.DaliySentence;
import com.ld.shieldsb.common.core.util.JsoupUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * 
 * 时光网
 * 
 * @author <a href="mailto:donggongai@126.com" target="_blank">吕凯</a>
 * @date 2020年1月8日 上午11:00:51
 *
 */
@Slf4j
public class DaliySentenceMtime implements DaliySentence {
    public static final String URL = "http://www.mtime.com";

    @Override
    public String getSentence() {
        // 首页采用了js加密，需要发送2次请求，第一次请求获取加密的js从中取到cookie，第二次带着cookie发送请求,参考：https://blog.csdn.net/Nagisazz/article/details/84937239
        String content = null;

        Document document = JsoupUtil.getRemoteURLDocument(URL, null);
        if (document != null) {
            String docData = document.data().trim();
            log.debug(docData);
            String jsStr = document.selectFirst("script").data().trim();
            if (docData.equals(jsStr)) { // 说明只包含了js
                /**
                 * b原始内容示例： <html><body><script language="javascript"> window.onload=setTimeout("dy(12)", 200); function dy(OB) {var qo,
                 * mo="", no="", oo = [0x35,0x77,0x
                 * 96,0x88,0xd9,0xac,0xfe,0xeb,0xae,0xcf,0x96,0x88,0x7b,0x2e,0x34,0xa6,0x39,0x4b,0x21,0x57,0x0b,0xba,0x0c,0x6e,0x60,0x03,0x55,0x67,0x28,0x29,0x
                 * 3b,0xfd,0xef,0x02,0xd7,0x5d,0xf2,0x48,0x8a,0xff,0xc1,0x27,0xb0,0xc5,0x5b,0x80,0xa2,0x08,0x5d,0xb3,0xb8,0xbd,0x82,0xc4,0x27,0x3c,0x20,0x32,0x
                 * 57,0x4c,0x41,0xd6,0xeb,0xa4,0x7b,0xc0,0x16,0x58,0xbd,0x54,0xf2,0x18,0x3d,0x32,0x97,0x6e,0x70,0xad,0x13,0x28,0x6d,0x04,0x69,0x7e,0x49,0xee,0x
                 * 34,0x56,0x58,0xad,0xf2,0x01,0x43,0x58,0x9a,0x9f,0x76,0x4b,0x12,0x67,0xcc,0x32,0x77,0xcc,0x02,0x3c,0x31,0x06,0x1b,0x90,0xb6,0xc8,0x07,0xa8,0x
                 * 89,0x1c,0x1d,0x6f,0x60,0x31,0xc6,0x0b,0x0c,0x9e,0x45,0x6b,0xea,0xef,0xd4,0xab,0x80,0x52,0x53,0xe5,0xbc,0x81,0xa6,0x8c,0xb1,0x07,0xd9,0x5f,0x
                 * 84,0x99,0x1f,0x74,0x49,0xae,0x94,0xc8,0x9d,0x00,0x76,0x5c,0x2a,0x4c,0x3f,0xd1,0xe3,0x76,0x39,0xe8,0xbe,0x45,0x18,0x79,0xcb,0x9e,0x64,0xb6,0x
                 * 7d,0x6f,0x22,0xf4,0x6a,0xe8,0xce,0xef,0xc1,0x23,0x85,0x5b,0x6b,0x22,0x58,0xcd,0xf3,0x25,0xb7,0x1f,0xe1,0x04,0xf6,0x28,0xee,0x11,0xb8,0x6b,0x
                 * 5d,0xae,0x41,0x93,0x56,0xa6,0xc7,0x8e,0x71,0x24,0x16,0x28,0x76,0x97,0x2a,0xdc,0x9f,0x75,0x97,0xda,0x8d,0xbf,0xd0,0xa3,0xf5,0xb8,0xd1,0x33,0x
                 * f9,0x0d,0x10,0xd3,0x3b];qo = "qo=229; do{oo[qo]=(-oo[qo])&0xff; oo[qo]=(((oo[qo]>>6)|((oo[qo]<<2)&0xff))-116)&0xff;}
                 * while(--qo>=2);"; eval(qo);qo = 228; do { oo[qo] = (oo[qo] - oo[qo - 1]) & 0xff; } while (-- qo >= 3 );qo = 1; for (;;) {
                 * if (qo > 228) break; oo[qo] = ((((((oo[qo] + 82) & 0xff) + 81) & 0xff) << 2) & 0xff) | (((((oo[qo] + 82) & 0xff) + 81) &
                 * 0xff) >> 6); qo++;}po = ""; for (qo = 1; qo < oo.length - 1; qo++) if (qo % 7) po += String.fromCharCode(oo[qo] ^
                 * OB);eval("qo=eval;qo(po);");} </script> </body></html>
                 **/
                ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
                ScriptEngine engine = scriptEngineManager.getEngineByName("javascript");
                try {
                    String runString = getRunString(jsStr);
                    String fuction = runString.replace("eval(\"qo=eval;qo(po);\")", "");
                    engine.eval(fuction); // 执行js
                    String outStr = (String) engine.get("po"); // 最后执行的结果
                    String getCookieString = getCookieString(outStr); // 获取cookie
                    log.debug(outStr);
                    log.debug(getCookieString);
                    Map<String, String> headers = new HashMap<>();
//        headers.put("Cookie",
//                "waf_cookie=e06746e2-057d-401946677f0bf6ab50d422503a46a0917d47; _ydclearance=303496138285f0056b5fda95-c8c8-4ef1-ac76-31d9bbc0cc24-1584694596");
                    headers.put("Cookie", getCookieString); // 带上cookie
                    document = JsoupUtil.getRemoteURLDocument(URL, headers, null);
                } catch (Exception e) {
                    log.warn("", e);
                }

            }
            if (document != null) {
                Elements containerEle = document.select(".todayrecmd");
                Elements images = containerEle.select("img.bgposter"); // 图片
                content = "<img src=\"" + images.attr("data-src") + "\" width='270' >" + "<br/>"; // height='180'
                content += " “" + containerEle.select(".mainrecmd").text() + "” <br/>";
                content += "——《" + containerEle.select(".mtitle a").text() + "》" + containerEle.select(".mshow-time").text();
                content += "【来自<a href='http://www.mtime.com' target='_blank'>「时光网」</a>】";
            }
        }
        return content;
    }

    /**
     * 获取运行的js体
     * 
     * @Title getRunString
     * @author 吕凯
     * @date 2020年3月21日 上午11:08:05
     * @param html
     * @return String
     */
    private static String getRunString(String html) {
        html = html.trim();
//        Pattern pattern = Pattern.compile("(?<=window.onload=setTimeout\\(\".{1,20}?\", 200\\));");
        Pattern pattern = Pattern.compile("setTimeout\\(\".+?\\((.+?)\\)\", 200\\); function .+?\\((.+?)\\) \\{(.+)\\};?");
        Matcher matcher = pattern.matcher(html);
        String param1 = null;
        String param1Name = null;
        while (matcher.find()) {
            param1 = matcher.group(1);
            param1Name = matcher.group(2);
            String funStr = matcher.group(3);
            return funStr.replace(param1Name, param1);
        }
        return null;
    }

    /**
     * 获取cookie内容
     * 
     * @Title getCookieString
     * @author 吕凯
     * @date 2020年3月21日 上午11:08:24
     * @param html
     * @return String
     */
    private static String getCookieString(String html) {
        html = html.trim();
        Pattern pattern = Pattern.compile("document.cookie='(.+?)';");
        Matcher matcher = pattern.matcher(html);
        String param1 = null;
        while (matcher.find()) {
            param1 = matcher.group(1);
            return param1;
        }
        return null;
    }

    public static void main(String[] args) {
//        String ss = "window.onload=setTimeout(\"cw(148)\", 200); function cw(TJ) {var qo, mo=\"\", no=\"\", oo = [0xdc,0xdc,0x1e,0x6a,0x67,0xa3,0x51,0xcd,0x02,0x07,0x5d,0xa9,0x2d,0x79,0xa5,0xd1,0xed,0x6a,0xc5,0x84,0x13,0xe0,0x7d,0xe1,0xd4,0x21,0x65,0xe1,0x5e,0x33,0x80,0xdc,0x11,0x7d,0xf9,0xb8,0xf9,0x46,0xe4,0xc3,0x62,0xe6,0xc1,0xd8,0x4d,0xd1,0x3e,0x8a,0x27,0xcd,0x22,0x09,0x9d,0x7c,0x06,0x9a,0x39,0x8d,0xf9,0xd8,0x86,0xe2,0x7f,0x76,0x55,0x93,0xc6,0x4b,0x42,0x21,0xc7,0x06,0x29,0x30,0xd6,0xc5,0xd4,0x13,0x40,0x9c,0x73,0x5a,0x59,0x97,0xc8,0x45,0x5c,0x23,0x9f,0xf3,0x9f,0xeb,0x48,0x27,0xab,0x52,0x2b,0x12,0x66,0xa4,0x83,0x82,0x6c,0x13,0x1a,0x09,0xef,0xde,0xe7,0xd6,0xbd,0x94,0x23,0x89,0x73,0xef,0x94,0x79,0x95,0x6a,0x8a,0x07,0xf3,0xb2,0x28,0x1d,0x9e,0xba,0x00,0x47,0x3e,0x05,0x99,0xd7,0x15,0x71,0x46,0x84,0x1e,0xf4,0xdb,0x22,0x01,0xff,0xd3,0x6a,0x41,0x40,0xd6,0xdd,0xea,0xc1,0x28,0xb5,0xf2,0xf8,0xcf,0x7e,0xc4,0x49,0x95,0xd1,0xa8,0x05,0x21,0x75,0x34,0x6a,0xb1,0xed,0xf2,0x0f,0x4b,0xc7,0xe6,0x3d,0x89,0xd5,0x12,0xc0,0x5e,0xa4,0x89,0xe5,0xea,0x0f,0x65,0x24,0x72,0x00,0x8f,0xf5,0xf4,0xe1,0x1e,0x52,0xd6,0x03,0xd9,0xc6,0x1d,0x81,0xcd,0x3a,0x75,0x72,0xae,0x2b,0x7f,0x64,0x6c,0xa2,0xe6,0x33,0x9f,0xfb,0xe1,0xe6,0x03,0x4f,0x83,0x42,0x24,0xa8,0xf4,0x41,0x5e,0x9a,0xe7,0x64,0xb8,0xbd,0xf3,0xf1,0xf2,0xc8,0xd6,0x3b];qo = \"qo=236; do{oo[qo]=(-oo[qo])&0xff; oo[qo]=(((oo[qo]>>5)|((oo[qo]<<3)&0xff))-121)&0xff;} while(--qo>=2);\"; eval(qo);qo = 235; do { oo[qo] = (oo[qo] - oo[qo - 1]) & 0xff; } while (-- qo >= 3 );qo = 1; for (;;) { if (qo > 235) break; oo[qo] = ((((((oo[qo] + 91) & 0xff) + 5) & 0xff) << 2) & 0xff) | (((((oo[qo] + 91) & 0xff) + 5) & 0xff) >> 6); qo++;}po = \"\"; for (qo = 1; qo < oo.length - 1; qo++) if (qo % 6) po += String.fromCharCode(oo[qo] ^ TJ);eval(\"qo=eval;qo(po);\");}";

//        System.out.println(getRunString(ss));
        DaliySentenceMtime test = new DaliySentenceMtime();
        System.out.println(test.getSentence());
    }

}
